import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;


public class PeerWait extends Thread{
	
	PeerData peerData; // This will have peer variables like peerID, peer server name and other buffers we might have to use
	Message recievedMsg;
	Socket socket = null;
	private ObjectInputStream objIn;
	private ObjectOutputStream objOut;
	public void initPeerThread(){
		String THREADTYPE = "WAIT"; 
		
		//hard coded variables

		//Start Up Code
		try {
			ServerSocket serverSoc = new ServerSocket(peerData.getPortNumber()); 
			socket = serverSoc.accept();
			objIn = new ObjectInputStream(socket.getInputStream());
			objOut = new ObjectOutputStream(socket.getOutputStream());
			
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} //Connecting to peer with peerID < peerProp.peerID
		
		
		HandshakeMessage hMsg = new HandshakeMessage(objIn,objOut);
		hMsg.sendMessage();
		if(hMsg.recvMessage()){
		if(recievedMsg.getType() == MessageType.BITFIELD.getValue() ) //bit field type
		{	
			BitfieldMessage bMsg = new BitfieldMessage(objIn,objOut);
			bMsg.sendMessage();
			recievedMsg =  bMsg.recvMessage();
			boolean isInterested = peerData.checkBitfield();
			if(isInterested){
				InterestedMessage iMsg = new InterestedMessage(objIn,objOut);
				iMsg.sendMessage();
			}
		}
		}
	}
}
